package ua.flatmap.processor;

import com.flatsmap.dao.StreetDao;
import com.flatsmap.service.PoiBaseService;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Field;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.util.Version;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Kostya
 * Date: 14.07.11
 */
public abstract class XMLReader {
    protected Map<String, Float> streets;
    protected PoiBaseService poiService;
    protected StreetDao streetDao;

    protected static org.apache.lucene.document.Document createDocument(String content) {
        org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document();
        int stIndex = content.indexOf("ул.");// бул
        if (stIndex != -1) {
            int start = stIndex > 15 ? stIndex - 15 : 0;
            int end = stIndex + 25 < content.length() ? stIndex + 25 : content.length();
            content = content.substring(start, end);
        } else {
            stIndex = content.indexOf("пер.");
            if (stIndex != -1) {
                int start = stIndex > 15 ? stIndex - 15 : 0;
                int end = stIndex + 25 < content.length() ? stIndex + 25 : content.length();
                content = content.substring(start, end);
            }
        }
        doc.add(new Field("content", content, Field.Store.YES, Field.Index.ANALYZED));
        return doc;
    }


    protected Float search(IndexSearcher searcher, String queryString)
            throws ParseException, IOException {

        // Build a Query object
        QueryParser parser = new QueryParser(Version.LUCENE_32,
                "content",
                new StandardAnalyzer(Version.LUCENE_32));
        Query query = parser.parse(queryString);


        TopScoreDocCollector collector = TopScoreDocCollector.create(1, true);
        searcher.search(query, collector);

        ScoreDoc[] hits = collector.topDocs().scoreDocs;

        int hitCount = collector.getTotalHits();

        if (hitCount == 0) {
            return 0F;
        } else {

            for (int i = 0; i < hitCount; i++) {
                ScoreDoc scoreDoc = hits[i];
                float docScore = scoreDoc.score;

                return docScore;

            }
        }
        return 0F;
    }

    protected Map<String, Float> getStreets() {
        if (streets != null) {
            return streets;
        }
        streets = new HashMap<String, Float>();
        List<String> streetsList = streetDao.getStreetNames();
        for (String street : streetsList) {
            streets.put(street, null);
        }
        return streets;
    }

    protected String getNodeValue(Element element, String name) {
        NodeList nodeList = element.getElementsByTagName(name);
        Element element2 = (Element) nodeList.item(0);
        NodeList list = element2.getChildNodes();
        return ((Node) list.item(0)).getNodeValue().trim();
    }

    public PoiBaseService getPoiService() {
        return poiService;
    }

    public void setPoiService(PoiBaseService poiService) {
        this.poiService = poiService;
    }

    public void setStreetDao(StreetDao streetDao) {
        this.streetDao = streetDao;
    }

    public abstract void execute(String result);

}
